package com.demo.caizi.schedule;

import com.demo.caizi.biz.BizService;
import com.demo.caizi.biz.TradeService;
import com.demo.caizi.biz.domain.CustomInfo;
import com.demo.caizi.biz.domain.TradeInfo;
import com.demo.caizi.config.context.CustomContext;
import com.demo.caizi.config.context.CustomContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import uk.co.jemos.podam.api.PodamFactory;

import java.util.concurrent.ThreadLocalRandom;

@Component
public class TradingJob {

    @Autowired
    TradeService tradeService;

    @Autowired
    PodamFactory podamFactory;

    @Scheduled(cron = "15 0/15 * * * ? ")
    public void callRoll(){
        try {
            boolean loop = ThreadLocalRandom.current().nextBoolean();
            CustomInfo customInfo = podamFactory.manufacturePojo(CustomInfo.class);
            CustomContext context = new CustomContext();
            context.setCustomInfo(customInfo);
            CustomContextHolder.setHolder(context);
            if (loop) {
                int i = ThreadLocalRandom.current().nextInt(6, 10);
                for (int j = 0; j < i; j++) {
                    TradeInfo tradeInfo = podamFactory.manufacturePojoWithFullData(TradeInfo.class);
                    tradeInfo.setTradeCount(1);
                    tradeService.singleTrans(tradeInfo);
                }

            }
            loop = ThreadLocalRandom.current().nextBoolean();
            if (loop) {
                int i = ThreadLocalRandom.current().nextInt(2, 4);
                for (int j = 0; j < i; j++) {
                    TradeInfo tradeInfo = podamFactory.manufacturePojoWithFullData(TradeInfo.class);
                    tradeService.batcheTrans(tradeInfo);
                }

            }
            loop = ThreadLocalRandom.current().nextBoolean();
            if (loop) {
                int i = ThreadLocalRandom.current().nextInt(2, 6);
                for (int j = 0; j < i; j++) {
                    TradeInfo tradeInfo = podamFactory.manufacturePojoWithFullData(TradeInfo.class);

                    tradeService.innerTrans(tradeInfo);
                }

            }
            loop = ThreadLocalRandom.current().nextBoolean();
            if (loop) {
                int i = ThreadLocalRandom.current().nextInt(2, 6);
                for (int j = 0; j < i; j++) {
                    tradeService.management();
                }

            }
        }finally {
            CustomContextHolder.clear();
        }


    }
}
